package com.ggh.jinjilive.views.drag;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;

public class DrawableLayout extends RelativeLayout implements View.OnTouchListener {
    int duration = 300;

    int startX = 0;
    int startY = 0;

    int startLeft = 0;
    int startTop = 0;

    final int screenWidth = getResources().getDisplayMetrics().widthPixels;
    final int screenHeight = getResources().getDisplayMetrics().heightPixels;

    int marginSide = 0;

    private OnClickListener onClickListener;

    public interface OnClickListener {
        void onClick();
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setMarginSide(int marginSide) {
        this.marginSide = marginSide;
    }

    public DrawableLayout(Context context) {
        super(context);
        setOnTouchListener(this);
    }

    public DrawableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnTouchListener(this);
    }

    public DrawableLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnTouchListener(this);
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    @SuppressLint("NewApi")
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            startX = (int) event.getRawX();
            startY = (int) event.getRawY();

            startLeft = v.getLeft();
            startTop = v.getTop();

            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
            lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
            lp.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
            lp.removeRule(RelativeLayout.ALIGN_PARENT_END);
            lp.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            lp.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
            lp.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            lp.removeRule(RelativeLayout.CENTER_IN_PARENT);
            lp.removeRule(RelativeLayout.CENTER_HORIZONTAL);
            lp.removeRule(RelativeLayout.CENTER_VERTICAL);
            setLayoutParams(lp);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (Math.abs((int) startLeft - v.getLeft()) < 5 & Math.abs((int) startTop - v.getTop()) < 5) {
                onClickListener.onClick();
            }

            int right = screenWidth - v.getRight();
            int left = v.getLeft();

            TranslateAnimation animation = null;
            if (right < left) {
                right = screenWidth - marginSide;
                left = right - v.getWidth();
                animation = new TranslateAnimation(v.getLeft() - left, 0, 0, 0);
            } else {
                left = marginSide;
                right = left + v.getWidth();
                animation = new TranslateAnimation(v.getLeft(), 0, 0, 0);
            }
            animation.setDuration(duration);
            v.startAnimation(animation);

            v.layout(left, v.getTop(), right, v.getBottom());
            startX = (int) event.getRawX();
            startY = (int) event.getRawY();
            v.postInvalidate();
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            int dx = (int) event.getRawX() - startX;
            int dy = (int) event.getRawY() - startY;
            int left = v.getLeft() + dx;
            int below = v.getBottom() + dy;
            int right = v.getRight() + dx;
            int top = v.getTop() + dy;
            // 下面判断移动是否超出屏幕
            if (left < 0) {
                left = 0;
                right = left + v.getWidth();
            }
            if (top < 0) {
                top = 0;
                below = top + v.getHeight();
            }
            if (right > screenWidth) {
                right = screenWidth;
                left = right - v.getWidth();
            }
            if (below > screenHeight) {
                below = screenHeight;
                top = below - v.getHeight();
            }

            v.layout(left, top, right, below);
            startX = (int) event.getRawX();
            startY = (int) event.getRawY();
            v.postInvalidate();
        }

        //在父容器中固定
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
        lp.alignWithParent = false;
        lp.setMargins(v.getLeft(), v.getTop(), 0, 0);
        setLayoutParams(lp);
        return true;
    }
}